'use strict';
const get = require('lodash/get');

module.exports = () => async function Auth(ctx, next) {
    const config = ctx.app.config;

    const result = await ctx.callAPI('FETCH_USER_INFO');
    const data = get(result,'res.data');

    // 根据获取的用户信息，若 token 不合法，则要求登录
    if(data.error_code !==0){
        ctx.cookies.set('x-token', null, { maxAge: 0, domain: '.test.seewo.com' });
        ctx.cookies.set('x-auth-token', null, { maxAge: 0, domain: '.test.seewo.com' });
        ctx.redirect(`${config.loginUrl}?redirect_url=${encodeURIComponent(`${config.baseUrl}`)}`);
    }else{
        await next();
    }
}